package com.atguigu.test.processor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;

/**
 * @author zp
 * @version 1.0
 * @description: 自定义生命周期 bean增强器
 * @date 2022/11/28 22:09
 */
@Slf4j
public class MyLifeCycleBeanPostProcessor implements DestructionAwareBeanPostProcessor, InstantiationAwareBeanPostProcessor {

    @Override
    public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
        if("lifeCycleBean".equals(beanName)){
            log.info("postProcessBeforeDestruction》》》lifeCycleBean 销毁之前执行");
        }
    }

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if("lifeCycleBean".equals(beanName)){
            log.info("postProcessBeforeInstantiation》》》lifeCycleBean 实例化之前执行");
        }
        return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInstantiation(beanClass, beanName);
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if("lifeCycleBean".equals(beanName)){
            log.info("postProcessAfterInstantiation》》》lifeCycleBean 实例化之后执行");
        }
        return InstantiationAwareBeanPostProcessor.super.postProcessAfterInstantiation(bean, beanName);
    }

    @Override


    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        if("lifeCycleBean".equals(beanName)){
            log.info("postProcessProperties》》》lifeCycleBean 依赖注入 @Resource、@Autowired 等注解就在这个增强的");
        }
        return InstantiationAwareBeanPostProcessor.super.postProcessProperties(pvs, bean, beanName);
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if("lifeCycleBean".equals(beanName)){
            log.info("postProcessBeforeInitialization》》》lifeCycleBean 初始化之前");
        }
        return DestructionAwareBeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if("lifeCycleBean".equals(beanName)){
            log.info("postProcessAfterInitialization》》》lifeCycleBean 初始化之后");
        }
        return DestructionAwareBeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}
